<template>
  <div>
    <el-dialog :title="title" :visible="showDialog" @close="close">
      <el-form ref="formRef" label-width="100px" :model="form" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" placeholder="2-10个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" placeholder="2-10个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="managerId">
          <el-select
            v-model="form.managerId"
            placeholder="请选择部门负责人"
            style="width: 100%"
          >
            <el-option
              v-for="item in simpleList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="form.introduce"
            placeholder="请输入部门介绍"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
        <el-button @click="close">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reqGetSimpleList, reqAddDept, reqEditDept, reqGetDeptInfo } from '@/api/department'

export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    sourceDepts: {
      type: Array,
      default: () => []
    },
    pid: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      title: '新增部门',
      simpleList: [], // 部门负责人列表
      deptInfo: {

      },
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        managerId: '', // 部门负责人
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          {
            required: true,
            message: '部门名称是必填的',
            trigger: 'change'
          },
          {
            min: 2,
            max: 10,
            message: '部门名称在2-10个字符之间',
            trigger: 'change'
          },
          {
            validator: (rule, value, cb) => {
              // 判断编辑时回显的数据是合格的，
              if (this.form.id && value === this.deptInfo.name) {
                cb()
              }
              // 新增部门名称不允许和已经存在的部门名称一致
              this.sourceDepts.find((item) => item.name === value)
                ? cb(new Error(`已经存在${value}部门名称`))
                : cb()
            }
          }
        ],
        code: [
          {
            required: true,
            message: '部门编码是必填的',
            trigger: 'change'
          },
          {
            min: 2,
            max: 10,
            message: '部门编码在2-10个字符之间',
            trigger: 'change'
          },
          {
            validator: (rule, value, cb) => {
              if (value === this.deptInfo.code) {
                return cb()
              }
              this.sourceDepts.find((item) => item.code === value)
                ? cb(new Error(`已经存在${value}部门编码`))
                : cb()
            }
          }
        ],
        managerId: [
          {
            required: true,
            message: '部门负责人是必填的',
            trigger: 'change'
          }
        ],
        introduce: [
          {
            required: true,
            message: '部门介绍是必填的',
            trigger: 'change'
          },
          {
            min: 1,
            max: 100,
            message: '部门介绍在1-100个字符之间',
            trigger: 'change'
          }
        ]
      }
    }
  },
  async created() {
    const { data } = await reqGetSimpleList()
    this.simpleList = data
  },
  mounted() {},
  methods: {
    close() {
      this.$emit('update:showDialog', false)
      this.$refs.formRef.resetFields()
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        managerId: '', // 部门负责人
        introduce: '' // 部门介绍
      }
    },
    // 添加部门和修改部门
    handleSubmit() {
      this.$refs.formRef.validate(async(boo) => {
        if (boo) {
          if (this.form.id) {
            await reqEditDept(this.form)
          } else {
            await reqAddDept({ ...this.form, pid: this.pid })
          }
          this.close()
          //   通知父组件重新获取一次
          this.$emit('reload')
        }
      })
    },
    // 获取部门详情
    async showEditDept() {
      const { data } = await reqGetDeptInfo(this.pid)

      this.form = data
      this.deptInfo = { ...data }
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-dialog__footer {
  text-align: center;
}
</style>
